package net.maku.module.service.impl;

import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.framework.security.user.SecurityUser;
import net.maku.module.dao.AgApproveDao;
import net.maku.module.dao.AgCategoryDao;
import net.maku.module.dao.AgCategoryPermissionDao;
import net.maku.module.dao.AgContentDao;
import net.maku.module.entity.AgApproveEntity;
import net.maku.module.entity.AgCategoryEntity;
import net.maku.module.entity.AgCategoryPermission;
import net.maku.module.entity.AgContent;
import net.maku.module.service.AgApproveService;
import net.maku.module.service.AgCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;


@Service
public class AgApproveServiceImpl extends BaseServiceImpl<AgApproveDao, AgApproveEntity> implements AgApproveService {

    @Resource
    private AgApproveDao agApproveDao;

    @Resource
    private AgCategoryPermissionDao agCategoryPermissionDao;

    @Autowired
    private AgCategoryDao agCategoryDao;

    @Autowired
    private AgContentDao agContentDao;


    @Override
    public String addApprove(String businessType, Long businessId) {
        //String approveState,
        String approveType = "";
        Long categoryId = null;
        //取栏目表类型
        if ("1".equals(businessType)) {
            AgCategoryEntity agCategoryEntity = agCategoryDao.selectById(businessId);
            approveType = agCategoryEntity.getApproveType();
            categoryId = businessId;
        }
        //取内容表类型
        if ("2".equals(businessType)) {
            AgContent agContent = agContentDao.selectById(businessId);
            approveType = agContent.getApproveType();
            categoryId = Long.valueOf(agContent.getCategoryId());
        }

        //查询当前用户栏目权限
        long executor = SecurityUser.getUser().getId();
        AgApproveEntity agApproveEntity = new AgApproveEntity();
        agApproveEntity.setCreator(Math.toIntExact(executor));
        Map param = new HashMap();
        param.put("categoryId", categoryId);
        param.put("userId", executor);
        List<AgCategoryPermission> agCategoryPermissions = agCategoryPermissionDao.queryList(param);

        agApproveEntity.setApproveState("2");
        agApproveEntity.setApproveType(approveType);
        agApproveEntity.setBusinessType(businessType);
        agApproveEntity.setBusinessId(Math.toIntExact(businessId));
        agApproveEntity.setCreateTime(new Date());

        if (CollectionUtils.isEmpty(agCategoryPermissions)) {
            //管理员无需审核，直接通过,更新业务表状态
            String msg = updateBusinessInfo(agApproveEntity.getBusinessType(),
                    Long.valueOf(agApproveEntity.getBusinessId()), "4",
                    agApproveEntity.getApproveType());
            return msg;

        } else {
            String columnPermissions = agCategoryPermissions.get(0).getColumnPermissions();
            if ("1".equals(columnPermissions)) {
                //一级栏目管理员默认通过
                String msg = updateBusinessInfo(agApproveEntity.getBusinessType(),
                        Long.valueOf(agApproveEntity.getBusinessId()), "4",
                        agApproveEntity.getApproveType());
                return msg;

            }

            agApproveEntity.setNextNode(String.valueOf(Long.parseLong(columnPermissions) - 1));
            //获取当前节点
            String curNode = getCurNode(agApproveEntity);
            if ("0".equals(curNode)) {
                //无人审核修改状态
                String msg = updateBusinessInfo(agApproveEntity.getBusinessType(),
                        Long.valueOf(agApproveEntity.getBusinessId()), "4",
                        agApproveEntity.getApproveType());
                return msg;

            }
            agApproveEntity.setCurrentNode(curNode);
            agApproveEntity.setNextNode(String.valueOf(Long.parseLong(curNode) - 1));
            updateBusinessInfo(agApproveEntity.getBusinessType(),
                    Long.valueOf(agApproveEntity.getBusinessId()), agApproveEntity.getApproveState(),
                    agApproveEntity.getApproveType());

        }

        agApproveDao.insert(agApproveEntity);
        return "";

    }

    @Override
    public List<AgApproveEntity> queryList(Map param1) {
        return agApproveDao.queryList(param1);
    }

    @Override
    public void approve(AgApproveEntity agApprove) {
        long executor = SecurityUser.getUser().getId();
        AgApproveEntity agApproveEntity = agApproveDao.selectById(agApprove.getId());
        if (!"2".equals(agApproveEntity.getApproveState())) {
            //数据已经审核过了
            return;

        }
        if ("5".equals(agApprove.getApproveState())) {
            //审批驳回
            agApproveEntity.setApproveRemark(agApprove.getApproveRemark());
            agApproveEntity.setApproveState(agApprove.getApproveState());
            agApproveEntity.setUpdater(Math.toIntExact(executor));
            agApproveDao.updateById(agApproveEntity);
            updateBusinessInfo(agApproveEntity.getBusinessType(),
                    Long.valueOf(agApproveEntity.getBusinessId()), agApproveEntity.getApproveState(),
                    agApproveEntity.getApproveType());
            return;

        }
        if ("4".equals(agApprove.getApproveState())) {
            if ("0".equals(agApproveEntity.getNextNode())) {
                //最后一步
                agApproveEntity.setApproveRemark(agApprove.getApproveRemark());
                agApproveEntity.setApproveState(agApprove.getApproveState());
                agApproveEntity.setUpdater(Math.toIntExact(executor));
                agApproveDao.updateById(agApproveEntity);
                updateBusinessInfo(agApproveEntity.getBusinessType(),
                        Long.valueOf(agApproveEntity.getBusinessId()), agApproveEntity.getApproveState(),
                        agApproveEntity.getApproveType());
                return;
            }
            agApproveEntity.setApproveRemark(agApprove.getApproveRemark());
            agApproveEntity.setApproveState(agApprove.getApproveState());
            agApproveEntity.setUpdater(Math.toIntExact(executor));
            agApproveDao.updateById(agApproveEntity);

            //获取当前节点
            String curNode = getCurNode(agApproveEntity);
            if ("0".equals(curNode)) {
                //无人审核修改状态
                updateBusinessInfo(agApproveEntity.getBusinessType(),
                        Long.valueOf(agApproveEntity.getBusinessId()), agApproveEntity.getApproveState(),
                        agApproveEntity.getApproveType());
                return;

            }
            //判断节点是否有用户审核
            agApproveEntity.setId(null);
            agApproveEntity.setCurrentNode(curNode);
            agApproveEntity.setNextNode(String.valueOf(Long.parseLong(curNode) - 1));
            agApproveEntity.setCreator(Math.toIntExact(executor));
            agApproveEntity.setApproveState("2");
            agApproveDao.insert(agApproveEntity);

        }


    }

    private String getCurNode(AgApproveEntity agApproveEntity) {
        String businessType = agApproveEntity.getBusinessType();
        Integer categoryId = null;
        //取栏目表类型
        if ("1".equals(businessType)) {
            categoryId = agApproveEntity.getBusinessId();
        }
        //取内容表类型
        if ("2".equals(businessType)) {
            AgContent agContent = agContentDao.selectById(agApproveEntity.getBusinessId());
            categoryId = agContent.getCategoryId();
        }

        return getChildNode(categoryId, agApproveEntity.getNextNode());
    }

    private String getChildNode(Integer categoryId, String columnPermissions) {

        if ("0".equals(columnPermissions)) {
            return columnPermissions;
        }
        Map param = new HashMap();
        param.put("categoryId", categoryId);
        param.put("columnPermissions", columnPermissions);
        List<AgCategoryPermission> agCategoryPermissions = agCategoryPermissionDao.queryList(param);
        if (CollectionUtils.isEmpty(agCategoryPermissions)) {
            columnPermissions = Integer.parseInt(columnPermissions) - 1 + "";
            return getChildNode(categoryId, columnPermissions);
        } else {
            return columnPermissions;
        }
    }

    private String updateBusinessInfo(String businessType, Long businessId, String approveState, String approveType) {
        //栏目表
        if ("1".equals(businessType)) {
            if ("1".equals(approveType) || "2".equals(approveType)) {
                //新增
                AgCategoryEntity byId = agCategoryDao.selectById(businessId);
                byId.setApproveState(approveState);
                agCategoryDao.updateById(byId);
                return "";
            }
            if ("3".equals(approveType) && "4".equals(approveState)) {
                //删除
                agCategoryDao.deleteById(businessId);
                return "";

            }
            if ("3".equals(approveType) && "2".equals(approveState)) {
                //删除
                AgCategoryEntity byId = agCategoryDao.selectById(businessId);
                byId.setApproveState(approveState);
                agCategoryDao.updateById(byId);
                return "";

            }

        }

        //内容表
        if ("2".equals(businessType)) {
            if ("1".equals(approveType) || "2".equals(approveType)) {
                //新增
                AgContent agContent = agContentDao.selectById(businessId);
                agContent.setApproveState(approveState);
                agContentDao.updateById(agContent);
                return "";
            }
            if ("3".equals(approveType) && "4".equals(approveState)) {
                //删除
                agContentDao.deleteById(businessId);
                return "";

            }
            if ("3".equals(approveType) && "2".equals(approveState)) {
                //删除
                AgContent agContent = agContentDao.selectById(businessId);
                agContent.setApproveState(approveState);
                agContentDao.updateById(agContent);
                return "";

            }

        }
        return "";

    }


}
